//
//  AccountManger.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/1/28.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

public protocol AccountMangerViewDelegate {
    func clickSaveButton(account:AccountBean)
    func clickDeleteButton(linkmanid:Int)
}

class AccountMangerView: UIView {
    
    private var titleLabels = [UILabel]()
    private var inputTexts = [InputTextView]()
    private let titles = ["姓名","手机号","邮箱地址"]
    
    private let deleteLabel = UILabel(title: "删除联系人", color: UIColor.orange, size: 14, align: .center)
    
    // MARK: 赋值
    public var account:AccountBean?
    public var isShowDelete: Bool = false
    // MARK: 代理
    public var delegate:AccountMangerViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        init_views()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        // 宽度
        let w = frame.width - MARGIN * 2
        // 姓名
        let username = titleLabels[0]
        username.frame = CGRect(x: MARGIN, y: MARGIN, width: MARGIN50, height: MARGIN)
        // 姓名输入
        let usernameInput = inputTexts[0]
        usernameInput.placeholder = "请输入姓名"
        usernameInput.frame = CGRect(x: MARGIN, y: username.frame.maxY, width: w, height: MARGIN50)
        // 手机号
        let mobile = titleLabels[1]
        mobile.frame = CGRect(x: MARGIN, y: usernameInput.frame.maxY + MARGIN20, width: MARGIN50, height: MARGIN)
        // 手机号输入
        let mobileInput = inputTexts[1]
        mobileInput.placeholder = "请输入手机号"
        mobileInput.keyboardType = .numberPad
        mobileInput.frame = CGRect(x: MARGIN, y: mobile.frame.maxY, width: w, height: MARGIN50)
        // 邮箱
        let email = titleLabels[2]
        email.frame = CGRect(x: MARGIN, y: mobileInput.frame.maxY + MARGIN20, width: MARGIN50, height: MARGIN)
        // 邮箱输入
        let emailInput = inputTexts[2]
        emailInput.placeholder = "请输入邮箱地址"
        emailInput.frame = CGRect(x: MARGIN, y: email.frame.maxY, width: w, height: MARGIN50)
        // 保存按钮
        let saveBtn = UIButton(title: "保存", name: "button_orange",radius: 5)
        saveBtn.frame = CGRect(x: MARGIN, y: emailInput.frame.maxY + MARGIN30, width: SCREEN_WIDTH - MARGIN * 2, height: MARGIN50)
        addSubview(saveBtn)
        // 保存按钮 点击事件
        saveBtn.addTarget(self, action: #selector(clickSaveItem), for: .touchUpInside)
        // 删除按钮
        if isShowDelete {
            deleteLabel.frame = CGRect(x: 0, y: saveBtn.frame.maxY + MARGIN20, width: frame.width, height: MARGIN20)
            addSubview(deleteLabel)
            // 删除按钮 点击事件
            deleteLabel.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(clickDeleteItem)))
        }
        // 赋值
        if account != nil {
            emailInput.text = account?.email
            mobileInput.text = account?.mobile
            usernameInput.text = account?.username
        }
    }
    
    @objc func clickSaveItem() -> Void {
        let account = AccountBean()
        account.username = inputTexts[0].text
        account.mobile = inputTexts[1].text
        account.email = inputTexts[2].text
        delegate?.clickSaveButton(account: account)
    }
    
    @objc func clickDeleteItem() -> Void {
        delegate?.clickDeleteButton(linkmanid: account?.linkmanid ?? 0)
    }
    
    private func init_views() -> Void {
        for i in 0...2 {
            // 标题
            let label = UILabel()
            label.text = titles[i]
            label.textColor = COLOR_NICE
            label.font = UIFont.systemFont(ofSize: 12)
            titleLabels.append(label)
            // 输入
            let input = InputTextView()
            input.textColor = COLOR_NICE
            input.font = UIFont.systemFont(ofSize: 16)
            inputTexts.append(input)
            // 添加到父控件
            addSubview(label)
            addSubview(input)
        }
    }
    
}
